package com.tcm.common.handler;

import com.tcm.websocket.WebSocket;
import io.micrometer.core.lang.NonNullApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/9/14 21:10
 */
@NonNullApi
public class WebSocketHandler extends TextWebSocketHandler {

    @Autowired
    private WebSocket webSocket;

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        webSocket.handleOpen(session);
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        webSocket.handleMessage(session, message.getPayload());
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        webSocket.handleError(session, exception);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        webSocket.handleClose(session);
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}
